package ext.generic.util;

import com.ptc.netmarkets.model.NmObjectHelper;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.TempFileCleaner;

import java.io.*;

import java.net.URL;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import wt.session.SessionHelper;

import wt.util.*;
/**
 *向EXCEL文件写入数据的封装类
 */
public class ExportToExcel
{
	public static final int EXPORT_ROW_LIMIT = 65535;
	public static final String MODEL_RESOURCE = "com.ptc.netmarkets.model.modelResource";
	protected static final String COMMA_DELIM = ",";
	protected static final String TAB_DELIM = "\t";
	protected File f;
	FileOutputStream out;
	private String fileName;
	private XSSFWorkbook wb;
	private XSSFSheet ws;

	public ExportToExcel()
	{
		wb = new XSSFWorkbook();
		ws = wb.createSheet("data");
	}

	public void writeColumnHeaders(String s, String s1)
		throws Exception
	{
		List list = parseStringToList(s, s1);
		writeRowData(list, 0);
	}

	public void writeRowData(List list, int row)
		throws Exception
	{
		XSSFRow wRow = ws.getRow(row);
		if (wRow == null)
			wRow = ws.createRow(row);
		StringBuffer stringbuffer = new StringBuffer();
		String s;
		int column = 0;
		System.out.println("	row data count=" + list.size());
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			s = (String) iterator.next();
			/*
			if(stringbuffer.length() > 0)
			{
				stringbuffer.append(",");
			}
			s = escapeComma(s);
			*/
			XSSFCell cell = wRow.createCell(column);
			cell.setCellValue(s);
			column++;
		}
		//out.write(stringbuffer.toString());
		//out.write(LINE_TERMINATOR);
	}


	public static List parseStringToList(String s, String s1)
	{
		List list = null;
		if (s != null && s1 != null)
		{
			String as[] = s.split(Pattern.quote(s1));
			list = Arrays.asList(as);
		}
		return list;
	}

	static String escapeComma(String s)
	{
		if (s.contains("\""))
		{
			s = s.replaceAll("\"", "\"\"");
		}
		return (new StringBuilder()).append("\"").append(s).append("\"").toString();
	}

	public final void open(File file, NmCommandBean nmcommandbean)
		throws Exception
	{
		f = createExportTempFile(file);
		//TempFileCleaner.register(nmcommandbean.getRequest().getSession(), f);
		out = new FileOutputStream(f);
	}

	public URL getDownloadUrl(NmCommandBean nmcommandbean)
		throws Exception
	{
		return NmObjectHelper.constructOutputURL(f, getFileName());
	}

	protected File createExportTempFile(File file)
		throws IOException, WTException
	{
		File file1 = File.createTempFile("exportList", ".xlsx", file);
		System.out.println("	export temp file=" + file1.getAbsolutePath());
		return file1;
	}

	File getFile()
	{
		return f;
	}

	public String getFileName()
	{
		return fileName;
	}

	public void setFileName(String s)
	{
		fileName = s;
	}

	public void close()
		throws Exception
	{
		wb.write(out);
		out.flush();
		out.close();
	}

	public static final int getExportFullListLimit()
	{
		try
		{
			return WTProperties.getLocalProperties().getProperty("com.ptc.jca.exportRowLimit", 65535);
		}
		catch (IOException ioexception)
		{
			ioexception.printStackTrace();
		}
		return 65535;
	}

	public static final String getOverLimitMessage()
		throws WTException
	{
		String s = WTMessage.getLocalizedMessage("com.ptc.netmarkets.model.modelResource", "16", new String[]
				{ String.valueOf(getExportFullListLimit()) }, SessionHelper.getLocale());
		return s;
	}
}
